﻿using System.Collections.Generic;
using Unknown.Framework.Database;
using Unknown.Framework.Transform;
using Unknown.Source.Entity.NormalView;

namespace Unknown.Source.OracleSql.NormalView
{
    /// <summary>
    /// 表读操作处理类
    /// </summary>
    public class CascadeTableOperationHandler : AbstractReaderOperationHandler<CascadeTable>
    {
        private Dictionary<string, CascadeTable> _Dictionary = new Dictionary<string, CascadeTable>();
        /// <summary>
        /// 字典
        /// </summary>
        protected Dictionary<string, CascadeTable> Dictionary
        {
            get
            {
                return this._Dictionary;
            }
        }

        /// <summary>
        /// 转换
        /// </summary>
        /// <returns>结果</returns>
        protected override CascadeTable Transform()
        {
            CascadeTable result = null;

            this.FieldPrefix = null;
            Table table = new Table();

            table.Name = this.AssignString("Name", null);
            table.Remark = this.AssignString("Remark", null);

            this.FieldPrefix = "Column";
            Column column = new Column();
            column.Name = this.AssignString("Name", null);
            column.DataType = this.AssignString("DataType", null);
            column.Length = this.AssignInteger("Length", 0);
            column.Precision = this.AssignInteger("Precision", null);
            column.Scale = this.AssignInteger("Scale", null);
            column.Nullable = TextHandler.Boolean(this.AssignString("Nullable", null), true);
            column.Remark = this.AssignString("Remark", null);

            bool has = this.Dictionary.ContainsKey(table.Name);
            if (has)
            {
                CascadeTable cascadeTable = this.Dictionary[table.Name];
                cascadeTable.Columns.Add(column);
            }
            else
            {
                result = new CascadeTable();
                result.Table = table;
                result.Columns.Add(column);

                this.Dictionary.Add(table.Name, result);
            }

            return result;
        }
    }
}